package com.hongfan.sentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.hongfan.sentinel.exception.GlobeBlockException;
import com.hongfan.sentinel.openfeign.IOffice30OpenFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author zcs
 * @since 2023/3/31
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    IOffice30OpenFeignService iOffice30OpenFeignService;

    /**
     * QPS
     * 根据设置的流控规则QPS进行测试
     */
    @GetMapping("/userQPSMonitor")
    @SentinelResource(value = "userQPSMonitor", blockHandlerClass = GlobeBlockException.class, blockHandler = "blockHandler")
    public String userQPSMonitor() {
        return "名称为: " + iOffice30OpenFeignService.getUserId(841);
    }


    /**
     * 并发线程数
     * 根据设置的流控规则并发线程数进行测试
     */
    @GetMapping("/userThreadMonitor")
    @SentinelResource(value = "userThreadMonitor", blockHandlerClass = GlobeBlockException.class, blockHandler = "blockHandler")
    public String userThreadMonitor() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "名称为: " + iOffice30OpenFeignService.getUserId(841);
    }


}
